/**
 * 
 */
package com.nwpusp.model.session;

import java.util.ArrayList;

import android.util.Log;

import com.nwpusp.controller.actions.Action;
import com.nwpusp.controller.actions.GameActionListener;
import com.nwpusp.transmitter.Transmitter;

/**
 * @author JustinCao
 *
 */
public class MessageSession extends Session implements GameActionListener {
	private int	watchDogCnt = 0;
	private int timeOutCnt = 20;
	
	public MessageSession(ConnectionId cid, Transmitter trans, int timeOutCnt) {
		super(cid, trans);
		this.timeOutCnt = timeOutCnt;
	}
	
	synchronized public int getWatchDogCnt() {
		return watchDogCnt;
	}

	synchronized public void increaseWatchDogCnt() {
		this.watchDogCnt++;
	}
	
	public boolean isTimeOut() {
		return (decreaseWatchDog() == 0);
	}
	
	synchronized private int decreaseWatchDog() {
		return timeOutCnt--;
	}

	public boolean Send(ArrayList<Action> al)
	{
// for debug {
		int i = 1;
		for(Action a : al) {
			Log.d("JustinSession", Integer.toString(i++) + "Sendto " + Long.toString(cid.getCid()) + " a msg" + a.toString());	
		}
// } for debug
		if(trans != null) {
			return trans.Send(getConnectionId(), al);
		}
		return false;
	}

	@Override
	public void OnAction(ArrayList<Action> al) {
		Send(al);
	}
	
	synchronized public void resetWatchdog() {
		timeOutCnt = 20;
	}
}
